package com.suzhiliang.springbootlesson.redisConfigDemo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Copyright (C), 2015-2018, XXX有限公司
 *
 * @ClassName: RedisController
 * @Author: xmm
 * @Date: 2018/10/30 16:49
 * @Description: redis 测试控制层
 * @Version 1.0
 */
@RestController
@RequestMapping(value = "redisController")
public class RedisController {

    @Autowired
    private RedisTemplate redisTemplate;

    @RequestMapping("test")
    public String test(){

        //操作String类型的数据
        ValueOperations<String, String> valueStr = redisTemplate.opsForValue();
        //存储一条数据
        valueStr.set("goodsProdu","长安");
        //获取一条数据并输出
        String goodsName = valueStr.get("goodsProdu");
        System.out.println(goodsName);
        //存储多条数据
        Map<String,String> map = new HashMap<>();
        map.put("goodsName","福特汽车");
        map.put("goodsPrice","88888");
        map.put("goodsId","88");

        valueStr.multiSet(map);
        //获取多条数据
        System.out.println("========================================");
        List<String>list = new ArrayList<>();
        list.add("goodsName");
        list.add("goodsPrice");
        list.add("goodsId");
        list.add("goodsProdu");

        List<String> listKeys = valueStr.multiGet(list);
        for (String key : listKeys) {
            System.out.println(key);
        }
        return "success";
    }
}
